﻿using System.Collections.Specialized;
namespace System.Web
{
    public sealed class NetHttpApplicationState : NameObjectCollectionBase
    {
        private HttpStaticObjectsCollection _applicationStaticObjects;
        private NetHttpApplicationStateLock _lock;
        private HttpStaticObjectsCollection _sessionStaticObjects;

        internal NetHttpApplicationState()
            : this(null, null) { }
        internal NetHttpApplicationState(HttpStaticObjectsCollection applicationStaticObjects, HttpStaticObjectsCollection sessionStaticObjects)
            : base(NetHttpModuleCollection.CaseInsensitiveInvariantKeyComparer)
        {
            _lock = new NetHttpApplicationStateLock();
            _applicationStaticObjects = applicationStaticObjects;
            if (_applicationStaticObjects == null)
                _applicationStaticObjects = new HttpStaticObjectsCollection();
            _sessionStaticObjects = sessionStaticObjects;
            if (_sessionStaticObjects == null)
                _sessionStaticObjects = new HttpStaticObjectsCollection();
        }

        public void Add(string name, object value)
        {
            _lock.AcquireWrite();
            try { base.BaseAdd(name, value); }
            finally { _lock.ReleaseWrite(); }
        }

        public void Clear()
        {
            _lock.AcquireWrite();
            try { base.BaseClear(); }
            finally { _lock.ReleaseWrite(); }
        }

        internal void EnsureUnLock()
        {
            _lock.EnsureReleaseWrite();
        }

        public object Get(int index)
        {
            object o = null;
            _lock.AcquireRead();
            try { o = base.BaseGet(index); }
            finally { _lock.ReleaseRead(); }
            return o;
        }

        public object Get(string name)
        {
            object o = null;
            _lock.AcquireRead();
            try { o = base.BaseGet(name); }
            finally { _lock.ReleaseRead(); }
            return o;
        }

        public string GetKey(int index)
        {
            string s = null;
            _lock.AcquireRead();
            try { s = base.BaseGetKey(index); }
            finally { _lock.ReleaseRead(); }
            return s;
        }

        public void Lock()
        {
            _lock.AcquireWrite();
        }

        public void Remove(string name)
        {
            _lock.AcquireWrite();
            try { base.BaseRemove(name); }
            finally { _lock.ReleaseWrite(); }
        }

        public void RemoveAll()
        {
            Clear();
        }

        public void RemoveAt(int index)
        {
            _lock.AcquireWrite();
            try { base.BaseRemoveAt(index); }
            finally { _lock.ReleaseWrite(); }
        }

        public void Set(string name, object value)
        {
            _lock.AcquireWrite();
            try { base.BaseSet(name, value); }
            finally { _lock.ReleaseWrite(); }
        }

        public void UnLock()
        {
            _lock.ReleaseWrite();
        }

        public string[] AllKeys
        {
            get
            {
                string[] a = null;
                _lock.AcquireRead();
                try { a = base.BaseGetAllKeys(); }
                finally { _lock.ReleaseRead(); }
                return a;
            }
        }

        public NetHttpApplicationState Contents
        {
            get { return this; }
        }

        public override int Count
        {
            get
            {
                int count = 0;
                _lock.AcquireRead();
                try { count = base.Count; }
                finally { _lock.ReleaseRead(); }
                return count;
            }
        }

        public object this[int index]
        {
            get { return Get(index); }
        }

        public object this[string name]
        {
            get { return Get(name); }
            set { Set(name, value); }
        }

        internal HttpStaticObjectsCollection SessionStaticObjects
        {
            get { return _sessionStaticObjects; }
        }

        public HttpStaticObjectsCollection StaticObjects
        {
            get { return _applicationStaticObjects; }
        }
    }
}
